package com.ylb.rent.controller;

import com.ylb.rent.entity.Permission;
import com.ylb.rent.entity.RolePermission;
import com.ylb.rent.service.IPermissionService;
import com.ylb.rent.util.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @AUTHOR:游李滨
 * @DATE:2023/6/28 22:00
 */
@RestController
@RequestMapping("/permission")
public class PermissionController {

    @Qualifier("permissionServiceImpl")
    @Autowired
    IPermissionService permissionService;


    @GetMapping("/all")
    public String  getAll(){

        List<Permission> all = permissionService.getAll();
        return R.successJSONData("成功",all);
    }


    @GetMapping("/list/role")
    public String  getPermissionByRoleId(long roleId){

        List<Permission> listByRoleId = permissionService.getListByRoleId(roleId);
        return R.successJSONData("成功",listByRoleId);

    }

    @PostMapping("/add")
    public String insert(@RequestBody Permission permission){

        int insert = permissionService.insert(permission);

        return insert==0?R.errorJSONData():R.successJSONData(permission);
    }


    @PostMapping("/role/update")
    public String updateRolePermission(@RequestBody RolePermission rolePermission){

        int result = permissionService.updateRolePermission(rolePermission);

        return result==0?R.errorJSONData():R.successJSONData(rolePermission);

    }

    @PostMapping("/update")
    public String update(@RequestBody Permission permission){

        int result = permissionService.update(permission);

        return result==0?R.errorJSONData("修改失败"):R.successJSONData("修改成功");

    }

}
